package com.xtgg.main;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestParams;
import com.xtgg.R;
import com.xtgg.login.LoginActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.PreferencesUtils;
import com.xtgg.util.ToastUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SearchWebActivity extends BaseActivity implements OnClickListener {
	MyAdapter mAdapter;
	JSONArray mArray = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_web);

		((TextView) findViewById(R.id.center)).setText("寻人网站");
		findViewById(R.id.left).setOnClickListener(this);
		final PullToRefreshListView ptrlvHeadlineNews = (PullToRefreshListView) findViewById(R.id.listview);
		ptrlvHeadlineNews
				.setMode(com.handmark.pulltorefresh.library.PullToRefreshBase.Mode.DISABLED);
		ptrlvHeadlineNews
				.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
					}
				});

		ListView listview = ptrlvHeadlineNews.getRefreshableView();
		mAdapter = new MyAdapter();
		listview.setAdapter(mAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			}
		});
		
		sendHttpRequest();
	}

	class MyAdapter extends BaseAdapter{
		public int getCount() {
			return mArray != null ? mArray.size() : 0;
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			FoodHolder foodHold = null;

			if (convertView == null) {
				LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				foodHold = new FoodHolder();

				convertView = mInflater.inflate(R.layout.item_web, null);
				foodHold.name = (TextView) convertView.findViewById(R.id.name);
				foodHold.status = (TextView) convertView.findViewById(R.id.status);
				foodHold.content = (TextView) convertView.findViewById(R.id.content);

				convertView.setTag(foodHold);
			} else {
				foodHold = (FoodHolder) convertView.getTag();
			}

			JSONObject item = mArray.getJSONObject(position);

			foodHold.name.setText(item.getString("websitename"));
			foodHold.content.setText(item.getString("websitename"));
			foodHold.status.setText("已发布");
			foodHold.status.setTextColor(Color.parseColor("#fb4e24"));
			foodHold.status.setVisibility(View.INVISIBLE);
			
			
			if(position == 0)
			{
				foodHold.name.setText("中国寻人网");
				foodHold.content.setText("中国最大的寻人寻亲网站");
				foodHold.status.setText("已发布");
				foodHold.status.setTextColor(Color.parseColor("#fb4e24"));
			}
			else if(position == 1)
			{
				foodHold.name.setText("宝贝回家网");
				foodHold.content.setText("关爱儿童，同驻和谐");
				foodHold.status.setText("已发布");
				foodHold.status.setTextColor(Color.parseColor("#fb4e24"));
			}
			else if(position == 2)
			{
				foodHold.name.setText("走失儿童信息网");
				foodHold.content.setText("中国最大的寻人寻亲网站");
				foodHold.status.setText("已撤回");
				foodHold.status.setTextColor(Color.parseColor("#ccccd1"));
			}
			
			return convertView;
		}
	}

	public final class FoodHolder {
		public TextView name;
		public TextView status;
		public TextView content;
	}
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.left:
			finish();
			break;
		}
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				Resp ruser = JSON.parseObject(response, Resp.class);

				if (ruser.getState().equals("1")) {
					mArray = JSON.parseObject(response).getJSONArray("data");
					mAdapter.notifyDataSetChanged();
				} 
				else if (ruser.getState().equals("2")) 
				{showExitDialog();} 
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getMessage());
				}
			}
		};
		// -----------------------

		RequestParams params = new RequestParams();
		params.put("act", "getWebSiteList");
		MgqRestClient.get(Constants.INFOMGT, params, loginHandler);
	}
}
